/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_UserAccount.h"

//---------------------------------------------------------------------------

MWmi_Win32_UserAccount::MWmi_Win32_UserAccount(void)
{
	propertyCount = 14;
	Initialize();
}

MWmi_Win32_UserAccount::~MWmi_Win32_UserAccount(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------

MWmiInfo* MWmi_Win32_UserAccount::CreateChild(void)
{
	MWmi_Win32_UserAccount *elem;

	try{
		elem = new MWmi_Win32_UserAccount;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_UserAccount::DeleteChild(void)
{
	MWmi_Win32_UserAccount *elem;

	while(next!=NULL){
		elem = (MWmi_Win32_UserAccount*)next;
		next = elem->next;
		elem->next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_UserAccount::Initialize(void)
{
	xAccountType        = 0;
	xDescription        = _T("");
	xDisabled           = false;
	xDomain             = _T("");
	xFullName           = _T("");
	xLocalAccount       = false;
	xLockout            = false;
	xName               = _T("");
	xPasswordChangeable = _T("");
	xPasswordExpires    = _T("");
	xPasswordRequired   = _T("");
	xSID                = _T("");
	xSIDType            = 0xFF;
	xStatus             = _T("");
}

//---------------------------------------------------------------------------

CString MWmi_Win32_UserAccount::GetQueryString(void)
{
	CString str;

	str = _T("select * from Win32_UserAccount");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_UserAccount::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("AccountType");
			break;
		case 1 :
			str = _T("Description");
			break;
		case 2 :
			str = _T("Disabled");
			break;
		case 3 :
			str = _T("Domain");
			break;
		case 4 :
			str = _T("FullName");
			break;
		case 5 :
			str = _T("LocalAccount");
			break;
		case 6 :
			str = _T("Lockout");
			break;
		case 7 :
			str = _T("Name");
			break;
		case 8 :
			str = _T("PasswordChangeable");
			break;
		case 9 :
			str = _T("PasswordExpires");
			break;
		case 10 :
			str = _T("PasswordRequired");
			break;
		case 11 :
			str = _T("SID");
			break;
		case 12 :
			str = _T("SIDType");
			break;
		case 13 :
			str = _T("Status");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_UserAccount::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_I4) return false;
			xAccountType = vtIn->lVal;
			break;
		case 1 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDescription = vtIn->bstrVal;
			break;
		case 2 :
			if(vtIn->vt!=VT_BOOL) return false;
			xDisabled = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 3 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDomain = vtIn->bstrVal;
			break;
		case 4 :
			if(vtIn->vt!=VT_BSTR) return false;
			xFullName = vtIn->bstrVal;
			break;
		case 5 :
			if(vtIn->vt!=VT_BOOL) return false;
			xLocalAccount = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 6 :
			if(vtIn->vt!=VT_BOOL) return false;
			xLockout = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 7 :
			if(vtIn->vt!=VT_BSTR) return false;
			xName = vtIn->bstrVal;
			break;
		case 8 :
			if(vtIn->vt!=VT_BOOL) return false;
			xPasswordChangeable = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 9 :
			if(vtIn->vt!=VT_BOOL) return false;
			xPasswordExpires = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 10 :
			if(vtIn->vt!=VT_BOOL) return false;
			xPasswordRequired = (vtIn->boolVal==VARIANT_TRUE ? true : false);
			break;
		case 11 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSID = vtIn->bstrVal;
			break;
		case 12 :
			if(vtIn->vt!=VT_UI1) return false;
			xSIDType = (BYTE)vtIn->bVal;
			break;
		case 13 :
			if(vtIn->vt!=VT_BSTR) return false;
			xStatus = vtIn->bstrVal;
			break;

		default :
			return false;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_UserAccount::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_UserAccount *elem;
	MWmi_Win32_UserAccount *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_UserAccount*)wmiInfoIn;

	xAccountType        = elem->xAccountType;
	xDescription        = elem->xDescription;
	xDisabled           = elem->xDisabled;
	xDomain             = elem->xDomain;
	xFullName           = elem->xFullName;
	xLocalAccount       = elem->xLocalAccount;
	xLockout            = elem->xLockout;
	xName               = elem->xName;
	xPasswordChangeable = elem->xPasswordChangeable;
	xPasswordExpires    = elem->xPasswordExpires;
	xPasswordRequired   = elem->xPasswordRequired;
	xSID                = elem->xSID;
	xSIDType            = elem->xSIDType;
	xStatus             = elem->xStatus;

	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_UserAccount;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

bool MWmi_Win32_UserAccount::IsEmpty(void)
{
	return xName.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
